<?php

	class Item extends Main{
		private $connect;
		private $lst;
		private $form_cat = array ('Книги'=>1, 'Периодика'=>2, 'Фотография'=>3);
		private $list1 = array(0, 1, 2, 3, 4 ,6 ,7 ,8);
		private $list2 = array(0, 1, 2, 3, 4, 5, 8);
		private $list3 = array(0, 2, 3, 5, 9, 8);
		private $fields = array(
			"<tr><td><span>*&nbsp</span>Название: </td><td><input type=\"text\" name=\"title\" value=\"\" required=\"required\" /></td></tr>",				// заменить кавычки - в начале поставить ''
			"<tr><td>ISBN / ISSN: </td><td><input type=\"text\" name=\"isbn-sn\" value=\"\" /></td></tr>",
			"<tr><td>Дата выпуска: </td><td><input type=\"text\" name=\"issue_date\" value=\"\" /></td></tr>",
			"<tr><td>Автор: </td><td><input type=\"text\" name=\"author\" value=\"\" /></td></tr>",
			"<tr><td>Издательство: </td><td><input type=\"text\" name=\"publisher\" value=\"\" /></td></tr>",
			"<tr><td><span>*&nbsp</span>Краткое описание: </td><td><input type=\"text\" name=\"short_descr\" value=\"\" required=\"required\" /></td></tr>",
			"<tr><td><span>*&nbsp</span>Описание: </td><td><input type=\"text\" name=\"description\" value=\"\" required=\"required\" /></td></tr>",
			"<tr><td>Содержание: </td><td><textarea cols=\"50\" rows=\"10\" name=\"content\"></textarea></td></tr>",
			"<tr><td><span>*&nbsp</span>Цена:</td><td><input type=\"text\" name=\"price\" value=\"\"  required=\"required\" /></td></tr>",
			"<tr><td>Разрешение: </td><td><input type=\"text\" name=\"resolution\" value=\"\" /></td></tr>"
		);
		
		private $sqlQueryList = array(
			"title",
			"`isbn-sn`",
			"issue_date",
			"author",
			"publisher",
			"short_descr",
			"description",
			"content",
			"price",
			"resolution"
		);

		public function __construct(){
			$this->connect = parent::connect();
		}
		
		public function getForm($cf, $q){			// cat_ind ????
			if ($q){
				$fields = $this->getFields($cf);
				return $this->modify($fields, $q);
			}
			else {
				return $this->getFields($cf);
			}
		}
		
		private function modify($fields, $q){			//getFields
			$get="";

			foreach ($this->lst as $val){
				$get[] = $this->sqlQueryList[$val];	
			}

			$get = implode($get, ",");
			$select = 'SELECT '.$get.' FROM store WHERE id='.(int) $q.'';				// !!! Attention  Зашитый ID !!!!
			$row = mysql_query($select, $this->connect);
			if (!$row) die(mysql_error());							//  <-- тут можно вставить исключение  Exception SPL
			$result = mysql_fetch_array($row, MYSQL_NUM);

			$final;
			for ($i=0; $i<count($fields); $i++){
				if (strpos($fields[$i], 'value=""')){
					$final[] = str_replace('value=""', 'value="'.$result[$i].'"', $fields[$i]);
				}
				else{
					$final[] = str_replace('</textarea>', $result[$i].'</textarea>', $fields[$i]);
				}
			}

   			return $final;
   			
		}
		
		private function getFields($cf){	
			if ($cf==1){					// Убрать string значения
				$final = $this->getInput($this->list1); 			// убрать $this->fields  и  зашить его в getInput  непосредственно там где надо
				$this->lst = & $this->list1;
				return $final;
			}
			else if ($cf==2){
				$final = $this->getInput($this->list2);
				$this->lst = & $this->list2;
				return $final;
			}
			else if ($cf==3){
				$final = $this->getInput($this->list3);
				$this->lst = & $this->list3;
				return $final;
			}
			else {return FALSE;}
		}
		
		private function getInput($list){								//  	Field Selector
			$frm;
			foreach ($list as $value){								// SPL !!!!!!
				foreach($this->fields as $k=>$val){
					if ($value === $k){
						$frm[] = $val;
					}
				}
			}
			return $frm;
		}

		/*
		*	Category List in Product form
		*/
		public function form_filter($ford){
		$fopt="";
			foreach ($this->form_cat as $key=>$value){
			if (preg_match("/^$value$/", $ford)){	
					$fopt .= "<option value=\"$value\" selected=\"selected\">$key</option>";
				}
				else{
					$fopt .= "<option value=\"$value\">$key</option>";
				}
			}
			return $fopt;
		}
	}
?>